import java.io.File;
import java.time.chrono.IsoChronology;
import java.util.ArrayList;
import java.util.Scanner;
//import java.lang.String;


public class DocumentManager {
    public static void main(String[] args) throws Exception {
        Scanner sc=new Scanner(System.in);
        System.out.println(" --1:指定关键字检索文件  2:指定后缀名检索文件  \"\n" +
                "\t\t\t\t+ \"3:复制文件/目录  4:删除文件/目录  5:退出--");

        while (true){
            System.out.println("请输入指令：");
            int command=sc.nextInt();
            switch (command){
                case 1:
                    searchByKeyWorld();
                    break;
                case 2:
                    searchBySuffix();
                    break;

                    case 3:
                    copyDirectory();
                    break;

                    case 4:
                    deleteDir();
                    break;

                    case 5:
                    exit();
                    break;
                default:
                    System.out.println("您输入的指令有误！");
                    break;
            }
        }
    }

    private static void searchByKeyWorld(){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要检索的目录的位置：");

        String path=sc.next();
        File file=new File(path);

        if (!file.exists()||!file.isDirectory()){
            System.out.println(path+"(不是有效目录)");
            return;
        }

        System.out.println("请输入搜索关键字：");
        String key=sc.next();

        ArrayList<String> list=FileUtils.listFiles(file,key);


        for (Object obj:list){
            System.out.println(obj);
        }

    }
    private static void searchBySuffix() {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要检索的目录的位置：");

        String path=sc.next();
        File file=new File(path);

        if (!file.exists()||!file.isDirectory()){
            System.out.println(path+"(不是有效目录)");
            return;
        }

        System.out.println("请输入搜索后缀：");

        String suffix=sc.next();
        String[]suffixArray=suffix.split(",");

        ArrayList<String>list=FileUtils.listFiles(file,suffixArray);
        for (Object obj:list){
            System.out.println(obj);
        }

    }

    private static void copyDirectory() throws Exception {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入源目录：");
        String srcDirectory=sc.next();

        File srcFile =new File(srcDirectory);

        if (!srcFile.exists()||!srcFile.isDirectory()){
            System.out.println("无效目录！");
            return;
        }

        System.out.println("请输入目标位置：");
        String destDirectory =sc.next();
        File destFile=new File(destDirectory);

        if (!destFile.exists()||!destFile.isDirectory()){
            System.out.println("无效位置！");
            return;
        }

        FileUtils.copySrcPathToDestPath(srcFile,destFile);
    }

    private static void deleteDir(){
    Scanner sc=new Scanner(System.in);
        System.out.println("请输入需要删除的源目录：");
        String delPath=sc.next();

        File dir=new File(delPath);

        if (dir.exists()){
            File[]files=dir.listFiles();
            for (File file:files){
                if (file.isDirectory()){
                    deleteDir();
                }else {
                    file.delete();
                }
            }
            dir.delete();
        }
    }

    private static void exit(){
        System.out.println("您已经退出系统，谢谢使用！");
        System.exit(0);
    }
}
